<?php $this->_extends('../_layouts/default_layout'); ?>
<?php $this->_block('contents'); ?>
<link type="text/css" href="<?=Q::ini('custom_system/base_url')?>css/jq.mselect.css" rel="stylesheet" />
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.mselect.js?"></script>
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.multiselect.js"></script>
<script type="text/javascript">
$(function(){
	$(".datepicker").datepicker({dateFormat:'yy-mm-dd',changeMonth:true});
	var product_categories_data = <?=$product_categories?>;
	$(document).mselect(product_categories_data, 'product_categories_id', 'product_categories_id', "<?=(isset($_GET['product_categories_id']))?$_GET['product_categories_id']:0?>");
	$("#multiselect").multiselect({height: 260, minWidth: 160});
	$("#warehouse_multiselect").multiselect({height: 260, minWidth: 160});
});
</script>
<fieldset><legend>查询条件</legend>
<form method="get">
<? include(Q::ini('custom_system/elements_dir') . 'searchform_element.php');?>
<table class="form_table">
<tr>
	<th>&nbsp;</th>
	<th>出库日期</th>
	<td>
		<input name="begin_shipped_time" value="<?=isset($_GET['begin_shipped_time']) ? $_GET['begin_shipped_time'] : date('Y-m-01', CURRENT_TIMESTAMP)?>" class="datepicker" /> to
		<input name="end_shipped_time" value="<?=isset($_GET['end_shipped_time']) ? $_GET['end_shipped_time'] : date('Y-m-d', CURRENT_TIMESTAMP)?>" class="datepicker" />
	</td>
</tr>
<tr>
	<th>&nbsp;</th>
	<th>仓库</th>
	<td>
		<select name="warehouse_id[]" multiple="multiple" id="warehouse_multiselect">
		<?foreach ($warehouse as $w):?>
		<option value="<?=$w['id']?>" <?if (isset($_GET['warehouse_id']) && is_array($_GET['warehouse_id']) && in_array($w['id'], $_GET['warehouse_id']))echo "selected='selected'"?>><?=$w['name']?></option>
		<?endforeach;?>
		</select>
	</td>
</tr>
<tr>
	<th>&nbsp;</th>
	<th>网站/店铺</th>
	<td>
		<select name="shop_id[]" multiple="multiple" id="multiselect">
		<?foreach ($shop_group as $k => $sg):?>
		<optgroup label="<?=$k?>">
		<?foreach ($sg as $s):?>
		<option value="<?=$s['id']?>" <?if (isset($_GET['shop_id']) && is_array($_GET['shop_id']) && in_array($s['id'], $_GET['shop_id'])) echo "selected='selected'"?>><?=$s['name']?></option>
		<?endforeach;?>
		</optgroup>
		<?endforeach;?>
		</select>
	</td>
</tr>
<tr>
	<th>&nbsp;</th>
	<th>采购分类</th>
	<td><input type="hidden" name="product_categories_id" id="product_categories_id" /></td>
</tr>
<tr>
	<th>&nbsp;</th>
	<th>周期</th>
	<td>
		<label><input name="period" type="radio" value="daily" <?=(isset($_GET['period'])&&$_GET['period']=='daily')?"checked='checked'":''?> />日报表</label>&nbsp;&nbsp;
		<label><input name="period" type="radio" value="weekly" <?=(isset($_GET['period'])&&$_GET['period']=='weekly')?"checked='checked'":''?> />周报表</label>&nbsp;&nbsp;
		<label><input name="period" type="radio" value="monthly" <?=((isset($_GET['period'])&&$_GET['period']=='monthly')||!isset($_GET['period']))?"checked='checked'":''?> />月报表</label>
	</td>
</tr>
<tr>
	<td></td>
	<td><input type="hidden" name="is_search" value="1" /></td>
	<td><input type="submit" class="button" value="查询" /></td>
</tr>
</table>
</form></fieldset>

<?if (isset($result)):?>
<?$return_quantity = $product_quantity = $order_quantity = $product_amount = $shipping_fee = $discount = $attr_amount = $amount = $product_cost = $purchase_loss = $postage = 0;?>
<table width="100%" class="list_table">
<tr>
	<th>日期</th>
	<th>分类</th>
	<th>退货数量</th>
	<th>销售数量</th>
	<th>产品金额</th>
	<th>附加属性</th>
	<th>总金额</th>
	<th>产品成本</th>
	<th>采购损耗</th>
	<th>毛利</th>
	<th>毛利率</th>
</tr>
<?foreach ($result as $r):?>
<?php 
	$return_quantity += $r['return_quantity'];
	$product_quantity += $r['product_quantity'];
	$product_amount += $r['product_amount'];
	$attr_amount += $r['attr_amount'];
	$product_cost += $r['product_cost'];
	$purchase_loss += $r['product_cost']*0.05;
?>
<tr>
	<td title="<?=date('m.d',strtotime($r['begin'])).'-'.date('m.d',strtotime($r['end']))?>"><?=($_GET['period']=='daily')?date('Y-m-d', strtotime($r['begin'])):(($_GET['period']=='weekly')?'第'.date('W',strtotime($r['begin'])).'周':date('Y-m',strtotime($r['begin'])))?></td>
	<td><?=$r['categories_name']?></td>
	<td><?=$r['return_quantity']?></td>
	<td><?=$r['product_quantity']?></td>
	<td><?=sprintf('%.2f',$r['product_amount'])?></td>
	<td><?=sprintf('%.2f',$r['attr_amount'])?></td>
	<td><?=sprintf('%.2f',$r['product_amount']+$r['attr_amount'])?></td>
	<td><?=sprintf('%.2f',$r['product_cost'])?></td>
	<td><?=sprintf('%.2f',$r['product_cost']*0.05)?></td>
	<td><?=sprintf('%.2f',$r['product_amount']+$r['attr_amount']-$r['product_cost']-$r['product_cost']*0.05)?></td>
	<td><?=($r['product_amount']+$r['attr_amount']) ? sprintf('%.2f',($r['product_amount']+$r['attr_amount']-$r['product_cost']-$r['product_cost']*0.05)/($r['product_amount']+$r['attr_amount'])*100).'%':'-'?></td>
</tr>
<?endforeach;?>
<tr>
	<td colspan="2" align="right">总计（RMB）：</td>
	<td><?=$return_quantity?></td>
	<td><?=$product_quantity?></td>
	<td><?=sprintf('%.2f', $product_amount)?></td>
	<td><?=sprintf('%.2f', $attr_amount)?></td>
	<td><?=sprintf('%.2f', $product_amount+$attr_amount)?></td>
	<td><?=sprintf('%.2f', $product_cost)?></td>
	<td><?=sprintf('%.2f', $purchase_loss)?></td>
	<td><?=sprintf('%.2f', $product_amount+$attr_amount-$product_cost-$purchase_loss)?></td>
	<td><?=($product_amount+$attr_amount)?sprintf('%.2f', ($product_amount+$attr_amount-$product_cost-$purchase_loss)/($product_amount+$attr_amount)*100).'%':'-'?></td>
</tr>
</table>
<?endif;?>
<?php $this->_endblock();?>